package com.thinkwide.data.exchange;

import com.thinkwide.data.security.MFJSEncode;
import com.thinkwide.data.core.MFDate;
import com.thinkwide.data.core.MFPropperties;
import com.thinkwide.data.core.MFString;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class MFWebWeather {

    public static String weather_server = "weather.server";
    public static String weather_use_proxy = "weather.use_proxy";

    private List<MFWeather> weathers;

    public MFWebWeather() {
        weathers = new ArrayList<MFWeather>();
    }

    public void setWeathers(List<MFWeather> weathers) {
        this.weathers = weathers;
    }

    public List<MFWeather> getWeathers() {
        return weathers;
    }

    public void getWeather(int count, String city) throws Throwable {
        MFPropperties prop = new MFPropperties("weather.properties", true);
        String server = prop.getPropValue(weather_server);
        String use_proxy = prop.getPropValue(weather_use_proxy);
        String str = "";

        String new_city = MFJSEncode.UrlDecode(city, "utf-8");
        String url = String.format(server, count, new_city);

        if (MFString.compareNoCase(use_proxy, "true"))
            str = MFWebGet.getWebHtmlByProxy(url, "gbk");
        else
            str = MFWebGet.getWebHtml(url, "gbk");

        int i = str.indexOf("]=[");
        int j = str.indexOf("];", i);
        String strb = str.substring(i + 2, j + 1);

        JSONArray list = new JSONArray(strb);
        weathers.clear();
        for (int index = 0; index < list.length(); index++) {
            JSONObject jo = list.getJSONObject(index);
            MFWeather w = new MFWeather();

            w.setTemperature(jo.getString("t1") + "℃~" + jo.getString("t2") + "℃");
            w.setWeather(jo.getString("s1"));
            w.setWind(jo.getString("d1"));
            w.setWindLevel(jo.getString("p1"));

            weathers.add(w);
        }

    }

    public String translateWeatherPic(String value) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("晴", "0");
        map.put("晴转多云", "1");
        map.put("多云", "2");
        map.put("小到中雨", "8");
        map.put("阴", "2");
        map.put("小雨", "8");
        map.put("大雨", "12");
        map.put("雨夹雪", "6");
        map.put("小雪", "15");
        map.put("大雪", "17");
        map.put("雷阵雨", "4");

        for (Map.Entry<String, String> item : map.entrySet()) {
            if (item.getKey().indexOf(value) >= 0) {
                return item.getValue() + ".png";
            }
        }
        return "0.png";
    }

    public String getWeatherHtml(int count, String city) throws Throwable {
        getWeather(count, city);
        for (MFWeather item : weathers) {
            item.getTemperature();
            item.getWeather();
            item.getWind();
            item.getWindLevel();
        }

        MFWeather today = weathers.get(0);
        MFWeather tomorrow = weathers.get(1);

        today.setPicture(translateWeatherPic(today.getWeather()));
        tomorrow.setPicture(translateWeatherPic(tomorrow.getWeather()));

        String stoday = MFDate.format(MFDate.getDateTime(), "MM-dd");
        String stomorrow = MFDate.format(MFDate.addDay(MFDate.getDateTime(), 1), "MM-dd");

        String html = "  <style>" +
                ".weather {font-family: 宋体;font-size:12px;}" +
                "</style>" +
                "<table class='weather' width='350' border='0'>" +
                "  <tr>" +
                "    <td width='70' height='47'>" + city + "<a href='city.jsp'>[定制]</a></td>" +
                "    <td width='50' rowspan='2'><img src='/integration/weather/images/" + today.getPicture() + "' width='50' height='50' /></td>" +
                "    <td width='75' rowspan='2'>今天 (" + stoday + ")</br>" + today.getTemperature() + "</br>" + today.getWeather() + "</td>" +
                "    <td width='50' rowspan='2'><img src='/integration/weather/images/" + tomorrow.getPicture() + "' width='50' height='50'/></td>" +
                "    <td width='70' rowspan='2'><p>明天(" + stomorrow + ")</br>" + tomorrow.getTemperature() + "</br>" + tomorrow.getWeather() + "</p>" +
                "    </td>" +
                "  </tr>" +
                "</table>";


        return html;

    }

}
